<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" class="examine">
      <el-form class="dialog-main">
        <el-form-item label="血糖仪编号:">
          <el-input
            v-model="form.equipmentCode"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属科室:">
          <el-select v-model="form.departmentId" placeholder="请选择所属科室">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.departmentName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="血糖仪mac地址:" class="bloodSugarUrl">
          <el-input v-model="form.mac"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="sendParameter">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  inject: ["reload"],
  props: {
    edialog: {
      type: Boolean,
      required: true,
    },
    list: {
      type: Object,
    },
    Type: {
      type: String,
    },
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        departmentId: "", //设备所属科室id
        departmentName: "", //设备所属科室名字
        equipmentCode: "", //血糖仪编号
        mac: "", //设备mac地址
      },
      departmentList: [],
      obj: {},
    };
  },
  mounted() {
    // 判断是修改还是添加
    console.log(this.Type);
    // console.log(this.list);
    if (this.edialog) {
      this.dialogVisible = true;
      let obj = JSON.stringify(this.list);
      this.form = JSON.parse(obj);
      if (!this.form.id) {
        this.form = {};
      }
      if (this.Type == "add") {
        this.form = {};
      }
      console.log(this.form);
    }
    let routeid = sessionStorage.getItem("routeID");
    let routeID = routeid ? JSON.parse(routeid)["/index"] : "";
    this.getDepartment(routeID);
    // console.log(this.getDepartmentAll(data));
  },
  methods: {
    // 查询所有科室
    getDepartment(val) {
      let data = {
        perId: val,
      };
      this.$api.getDepartment(data).then((res) => {
        this.departmentList = res.data.data;
      });
    },
    // 取消
    closeDialog() {
      this.dialogVisible = false;
      this.$parent.addEquipment(false);
    },

    // 确认添加 提交
    sendParameter() {
      let data = {
        equipmentCode: this.form.equipmentCode,
        mac: this.form.mac,
        departmentId: this.form.departmentId,
        id: this.form.id ? this.form.id : "",
      };
      let api = this.form.id ? "updateEquipmentInfo" : "addEquipmentInfo";
      this.$api[api](data)
        .then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success(res.message);
            this.form = {};
            this.dialogVisible = false;
            this.$parent.addEquipment(false);
            this.reload();
          }
        })
        .catch((err) => {
          this.dialogVisible = false;
          this.$parent.addEquipment(false);
          // this.$message.error("修改失败");
          // console.log(err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.examine {
  & >>> .el-dialog {
    .el-dialog__footer {
      background-color: rgba(1, 167, 240, 0.1);
      text-align: center;
    }
    .el-dialog__headerbtn {
      display: none;
    }
    max-width: 800px;
    .dialog-main {
      display: flex;
      // justify-content: center;
      align-items: center;
      flex-wrap: wrap;
      .el-form-item {
        width: 45%;
        display: flex;
        justify-content: center;
        align-content: center;
        .el-form-item__label {
          // text-align: left;
          width: 120px;
          margin: 0;
          // padding-left: 10px;
        }
        .el-form-item__content {
          // background: violet;
          .el-input__inner {
            width: 180px;
          }
        }
      }

      @media screen and (max-width: 1196px) {
        .el-form-item {
          width: 100%;
        }
      }
      .bloodSugarUrl {
        .el-form-item__label {
          width: 120px;
        }
        .el-form-item__content {
          // background: violet;
          // background:red;

          .el-input__inner {
            // width: 160px;
          }
        }
      }
    }
    @media screen and (max-width: 1400px) {
      .dialog-main {
        // padding: 0 10px;
        justify-content: space-between;
      }
    }
    @media screen and (max-width: 1196px) {
      .dialog-main {
        justify-content: center;
      }
    }
    .dialog-footer {
      text-align: center;
    }
  }
}
</style>>
